package com.cs.beneficios.web.action;
import com.cs.beneficios.business.entity.VflexDAO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.paoyer.administration.business.model.UserVO;
import java.sql.SQLException;
import java.util.Collection;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;

public class GrupoFamiliarAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    HttpSession session = request.getSession();
    UserVO user = (UserVO)session.getAttribute("userInfo");
    
    Collection cComunas=null;
    Collection cBarrios=null;
    Collection cZonas=null;
    
    String opcion = request.getParameter("Option");
    
    try
    {
      VflexDAO objValorFlexDAO = new VflexDAO(cf); 
      cComunas = objValorFlexDAO.getVflex("COMUNA");
      cBarrios = objValorFlexDAO.getVflex("BARRIO");
      cZonas = objValorFlexDAO.getVflex("ZONA");
      cf.closeConnection(objValorFlexDAO.getConn());
      request.setAttribute("ListComunas",cComunas);
      request.setAttribute("ListBarrios",cBarrios);
      request.setAttribute("ListZonas",cZonas);
      if((Object)opcion != null)
      {
        if(opcion.equals("zona"))
        {
          String codeZona = request.getParameter("codeZona");
          cComunas = objValorFlexDAO.getVflex("COMUNA",codeZona);
          cBarrios = objValorFlexDAO.getVflex("BARRIO");
          request.setAttribute("ListComunas",cComunas);
          request.setAttribute("ListBarrios",cBarrios);
          request.setAttribute("ListZonas",cZonas);
          request.setAttribute("zona",codeZona);
        }else if(opcion.equals("comuna"))
        {
          String codeComuna = request.getParameter("vflComuna");
          String codeZona = request.getParameter("codeZona");
          String zona = "";
          if(codeZona.equals(""))
          {
             zona = objValorFlexDAO.buscarPadre(codeComuna);
             request.setAttribute("zona",zona);
          }else
          {
             zona = codeZona;
             request.setAttribute("zona",codeZona);
          }
          cComunas = objValorFlexDAO.getVflex("COMUNA",zona);
          cBarrios = objValorFlexDAO.getVflex("BARRIO",codeComuna);
          request.setAttribute("comuna",codeComuna);
          request.setAttribute("ListComunas",cComunas);
          request.setAttribute("ListBarrios",cBarrios);
          request.setAttribute("ListZonas",cZonas);
         
        }else if(opcion.equals("barrio"))
        {
          String codeComuna = request.getParameter("vflComuna");
          String codeZona = request.getParameter("codeZona");
          String codeBarrio = request.getParameter("codeBarrio");
          cBarrios = objValorFlexDAO.getVflex("BARRIO");
          request.setAttribute("ListBarrios",cBarrios);
          cComunas = objValorFlexDAO.getVflex("COMUNA");
          request.setAttribute("ListComunas",cComunas); 
          String zona = "";
          String comuna = "";
          comuna = objValorFlexDAO.buscarPadre(codeBarrio);
          request.setAttribute("comuna",comuna);
          if(!comuna.equals(""))
            zona = objValorFlexDAO.buscarPadre(comuna);
          request.setAttribute("zona",zona);
          request.setAttribute("barrio",codeBarrio);
        }
      }
      
    }catch(SQLException e)
    {
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSUB - cwsub ********/");
    }
    return mapping.findForward("success");
  }
}